import {join} from 'path'
const shell = require('electron').shell
const spawn = require('child_process').spawn
const cliPath=join($dirname, '../cli.exe')
export async function send (command,params) {
	
	var c=command.split("/")

	var str=JSON.stringify(params)
	var cli=spawn(cliPath,[c[0].trim(),c[1].trim(),str])
    return new Promise((resolve, reject) => {
	    cli.stdout.on('data', function (data) {

			let obj={
					"errcode":0,
					"errmsg":data+""
				}
			try{
				obj=JSON.parse(data+"")
			}catch(e){
				
			}
	        resolve(obj)
	    });
	    cli.stderr.on('data', function (data) {
	    
	    	var m={
	    		"errcode":0,
	    		"errmsg":data+""
	    	}
	        resolve(m)
	    });
	    cli.on('exit', function (code) {
			/*
	    	var m={
	    		"errcode":0,
	    		"errmsg":"请求超时，请重试，"+code
	    	}
	        resolve(m)*/
	    });
	})
}

export async function upload () {
	setInterval(function(){

		var cli=spawn(cliPath,['packages','polling'])
		cli.stdout.on('data', function (data) {
	    	//console.log(data+"")
	       
	    });
	    cli.stderr.on('data', function (data) {
	    	//console.log(data+"")
	    	
	    });
	    cli.on('exit', function (code) {
	    	//console.log(code+"")
	    });
	},1000*60*10)
}

export async function openLink(url){
		shell.openExternal(url)
}

